package com.huobao.yunying.adapters;

import java.util.List;

import com.huobao.yunying.entity.DingdanDetail_good_list;
import com.huobao.yunying.entity.Gouwuche_spec;
import com.huobao.yunying.entity.Gouwuche_specification;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.zhangying.R;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Adapter_Dingdan_Detail extends QuickAdapter<DingdanDetail_good_list> {

	public Adapter_Dingdan_Detail(Context context) {
		super(context, R.layout.item_dingdandetail_item);
	}

	@Override
	protected void convert(BaseAdapterHelper arg0, DingdanDetail_good_list detail_good_list) {
		TextView tv_goods_name = arg0.getView(R.id.tv_goods_name);
		TextView tv_price = arg0.getView(R.id.tv_price);
		ImageView imgView = arg0.getView(R.id.imgView);
		LinearLayout llyt_guige = arg0.getView(R.id.llyt_guige);
		ImageUtils.loadimg(imgView, detail_good_list.goods_image);
		tv_goods_name.setText(detail_good_list.goods_name);
		tv_price.setText("￥" + detail_good_list.price + "\n" + "×" + detail_good_list.quantity);
		showGuige(llyt_guige, detail_good_list.specification);
	}

	private void showGuige(LinearLayout llyt_guige, List<Gouwuche_specification> specification) {
		if(specification == null)return;
		llyt_guige.removeAllViews();
		for (int i = 0; i < specification.size(); i++) {
			Gouwuche_specification gouwuche_specification = specification.get(i);
			LinearLayout layout = new LinearLayout(context);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setGravity(Gravity.CENTER_VERTICAL);
			LayoutParams layoutParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
			layoutParams.weight = 1;
			layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
			layout.addView(createNameTextView(gouwuche_specification.spec_1), layoutParams);

			layout.addView(createHLine());
			// 生成右边的规格
			LinearLayout linearLayout = new LinearLayout(context);
			linearLayout.setOrientation(LinearLayout.VERTICAL);
			List<Gouwuche_spec> spec = gouwuche_specification.spec;
			for (Gouwuche_spec gouwuche_spec : spec) {
				linearLayout.addView(createValueTextView(
						gouwuche_spec.spec_2 + "		" + gouwuche_spec.num + "*" + gouwuche_spec.price));
				linearLayout.addView(createLine());
			}
			layoutParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
			layoutParams.weight = 1;
			layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
			layout.addView(linearLayout, layoutParams);

			llyt_guige.addView(createLine());
			llyt_guige.addView(layout);
		}
	}

	private View createHLine() {
		View view = new View(context);
		view.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
		view.setBackgroundColor(Color.parseColor("#dddddd"));
		return view;
	}

	private View createLine() {
		View view = new View(context);
		view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 1));
		view.setBackgroundColor(Color.parseColor("#dddddd"));
		return view;
	}

	/**
	 * 生成标题文字
	 * 
	 * @param text
	 * @return
	 */
	private TextView createNameTextView(String text) {
		TextView textView = new TextView(context);
		textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		textView.setText(text);
		textView.setTextSize(16);
		// textView.setPadding(0, 8, 0, 8);
		return textView;
	}

	private TextView createValueTextView(String text) {
		TextView textView = new TextView(context);
		textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		textView.setText(text);
		textView.setTextSize(14);
		return textView;
	}
}
